package com.rms.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.rms.domain.Job;
import com.rms.domain.Resume;
import com.rms.domain.ResumeJob;
import com.rms.domain.Worker;
import com.rms.service.WorkerService;
import com.rms.utils.service.BaseService;

/**
 * Worker业务层
 * 
 * @author Zheng weiyu
 * @date 2019年7月30日
 */
public class WorkerServiceImpl extends BaseService implements WorkerService {
	private String path = "/uploadResume";// 文件保存路径

	@Override
	public void regist(Worker worker) throws Exception {
		
		if (worker.getPassword() == null || "".equals(String.valueOf(worker.getPassword()))) {
			// 密码不能为null
			throw new RuntimeException("密码不能为null");
		}
		if (worker.getPassword().length() < 6 || worker.getPassword().length() > 32) {
			// 密码长度6-32位数
			throw new RuntimeException("密码长度6~32位数");
		}
		Worker w = workerDao.get(worker.getId());
		if (w != null) {
			// 用户已存在
			throw new RuntimeException("用户已存在");
		}
		workerDao.save(worker);
	}

	/*
	 * 投递简历 
	 * 保存简历信息到resumeJob表上
	 * 1.判断t_resume_job内是否存在记录，
	 * 2.存在，则抛出异常
	 * 3.不存在，则插入数据
	 */
	@Override
	public void sendResumToCompany(Resume resume,int job_id) throws Exception{
//	
//		if(resume.getResumeJobs()==null) {
//			//若该简历未被投递
//		}
			//判断该简历是否已经投递过该工作
			List<ResumeJob> list = resume.getResumeJobs();
			Boolean flag = false;
			for(ResumeJob r:list) {
				if(r.getJobId()==job_id) {
					flag = true;
				}
				
			}
			if(!flag) {
				//若没有投过
				ResumeJob resumeJob = new ResumeJob();
				resumeJob.setResumeId(resume.getId());
				resumeJob.setResume(resume);
				resumeJob.setJobId(job_id);
				resumeJob.setState(0);
				resumeJobDao.save(resumeJob);
			}else {
				throw new RuntimeException("您已投过该公司!");
			}
		
	}

	/*
	 * 文件上传:若文件已存在，执行update，否则执行save
	 */
public Resume uploadResume(HttpServletRequest request) throws Exception {
		
		InputStream in = null;
		OutputStream out = null;
		String filePath = "";
		try {
			// 建立工厂类
			DiskFileItemFactory f = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(f);
			// 判断传输文件是否是multipart/form-data类型
			if (!upload.isMultipartContent(request)) {
				throw new RuntimeException("传输的文件不是multipart/form-data类型!");
			}
			// 读取文件
			List<FileItem> list = upload.parseRequest(request);
			for (FileItem it : list) {
				if (!it.isFormField()) {
					// 得到文件名
					String fileName = it.getName();
					// 得到输入流
					in = it.getInputStream();
					//得到类路径
					String savePath = request.getServletContext().getRealPath(path);
					System.out.println(path);
					System.out.println(savePath);
					File savePathFile = new File(savePath);
					if (!savePathFile.exists()) {
						// 创建目录
						savePathFile.mkdir();
					}
					// 创建文件输出流
					out = new FileOutputStream(savePath + "\\" + fileName);
					// 保存的文件名
					filePath = savePath + "\\" + fileName;
					int len = 0;
					byte[] buf = new byte[1024];
					while ((len = in.read(buf)) > 0) {
						out.write(buf, 0, len);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		}
		//临时存放数据t
		Resume t = new Resume();
		t.setPath(filePath);
		t.setWorkerId(((Worker) request.getSession().getAttribute("worker")).getId());
		//取得数据库中的数据r
		Resume r = resumeDao.findByWid(t.getWorkerId());
		if (r == null) {
			// 若数据库中不存在记录，插入记录
			resumeDao.save(t);
		} else {
			// 若数据库中存在记录，更新记录
			t.setId(r.getId());
			resumeDao.update(t);
		}
		return resumeDao.findByWid(t.getWorkerId());

	}

	@Override
	public List<Job> getJobList() {
		return jobDao.query();
	}

	// @Override
	// public Worker login(Worker worker, Class<Worker> clazz) throws Exception {
	// return (Worker) super.login(worker, clazz);
	// }

}
